package com.zmm.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zmm.weblog.common.domain.dos.ArticleCategoryRelDo;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface ArticleCategoryRelMapper extends BaseMapper<ArticleCategoryRelDo> {

    default int deleteByArticleId(Long articleId) {
        return delete(Wrappers.<ArticleCategoryRelDo>lambdaQuery()
                .eq(ArticleCategoryRelDo::getArticleId, articleId));
    }

    /**
     * 根据文章ID查询
     * @param articleId
     * @return
     */
    default ArticleCategoryRelDo selectByArticleId(Long articleId){
        return selectOne(Wrappers.<ArticleCategoryRelDo>lambdaQuery()
                .eq(ArticleCategoryRelDo::getArticleId, articleId));
    }

    default ArticleCategoryRelDo selectByCategoryId(Long categoryId){
        return selectOne(Wrappers.<ArticleCategoryRelDo>lambdaQuery()
                .eq(ArticleCategoryRelDo::getCategoryId, categoryId)
                .last("limit 1"));
    }

    default List<ArticleCategoryRelDo> selectBatchByArticleIds(List<Long> articleIds) {

        LambdaQueryWrapper<ArticleCategoryRelDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(ArticleCategoryRelDo::getArticleId, articleIds);
        return selectList(wrapper);
    }

    /**
     * 根据分类ID查询所有的关联记录
     * @param categoryId
     * @return
     */
    default List<ArticleCategoryRelDo> selectListByCategoryId(Long categoryId) {
        LambdaQueryWrapper<ArticleCategoryRelDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCategoryRelDo::getCategoryId, categoryId);
        return selectList(wrapper);
    }
}